package com.heima.home.controller;



import com.heima.home.entity.Users;
import com.heima.home.service.UserService;
import com.heima.home.util.UserContext;
import com.heima.home.dto.LoginRequest;
import com.heima.home.dto.RegisterRequest;
import com.heima.home.dto.UserInfoResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/users")
public class UserController {

    @Autowired
    private UserService userService;


    @PostMapping("/upload")
    public Map<String, Object> uploadImage(@RequestParam("image") MultipartFile file) {
        Map<String, Object> result = new HashMap<>();
        if (file.isEmpty()) {
            result.put("success", false);
            result.put("message", "文件为空");
            return result;
        }
        try {
            //项目根目录下创建 uploads 目录
            String uploadDir = System.getProperty("user.dir") + "/uploads/";
            File dir = new File(uploadDir);
            if (!dir.exists()) dir.mkdirs();

            //获取文件名
            String filename = System.currentTimeMillis() + "_" + file.getOriginalFilename();
            File dest = new File(dir, filename);
            file.transferTo(dest);

            //返回图片的访问路径
            String imageUrl = "/uploads/" + filename;
            result.put("success", true);
            result.put("url", imageUrl);
        } catch (Exception e) {
            result.put("success", false);
            result.put("message", "上传失败");
        }
        return result;
    }




    @PostMapping("/register")
    public ResponseEntity<Void> register(@Validated @RequestBody RegisterRequest request) {
        userService.register(request);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/login")
    public ResponseEntity<Map<String, String>> login(@Validated @RequestBody LoginRequest request) {
        String token = userService.login(request);
        Map<String, String> response = Collections.singletonMap("token", token);
        return ResponseEntity.ok(response);
    }

    @GetMapping("/profile")
    public ResponseEntity<UserInfoResponse> getUserProfile() {
        Integer currentUserId = UserContext.getCurrentUserId();
        UserInfoResponse userInfo = userService.getUserInfo(currentUserId);
        return ResponseEntity.ok(userInfo);
    }

    @PutMapping("/profile")
    public ResponseEntity<Void> updateUserProfile(@RequestBody Map<String, String> updateData) {
        Integer currentUserId = UserContext.getCurrentUserId();
        
        // 如果只是更新头像
        if (updateData.containsKey("avatar")) {
            userService.updateAvatar(currentUserId, updateData.get("avatar"));
        }
        
        return ResponseEntity.ok().build();
    }
    @PutMapping("/updateUserInfo")
    public int updateUserInfo(@RequestBody Users user){
        Integer currentUserId = UserContext.getCurrentUserId();
        user.setId(currentUserId);
        return userService.updateUserInfo(user);
    }
}